﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xCommon.Typelib;
using Yule8xModel;
using Yule8xModel.PageModels;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Agent
{
    public partial class Index : System.Web.UI.Page
    {
        protected AgentIndexPageModels.AgentIndexPageData pageData;
        protected WxUserInfoResult wxUser;
        protected int user_id;
        protected bool isDrawFreeDiamond;
        protected void Page_Load(object sender, EventArgs e)
        {
            // 推广代理ID
            int referral_id = RequestUtil.GetInteger("referral_id");
            
            user_id = RequestUtil.GetInteger("user_id");
            if (RuntimeDataUtil.CheckDebugModel() && user_id > 0)
            {
                wxUser = UserModels.GetWxUserInfo(user_id);
                if (null == wxUser)
                {
                    ResponseUtil.WriteEnd("user_id无效!");
                }
            }
            else
            {
                wxUser = LoginPageBase.GetLoginInfo();
                if (null == wxUser)
                {
                    Response.Redirect(WechatUrls.GetUserInfoUrl("agent", referral_id), true);
                }
            }
            /*
            // 初始化日志助手
            object loginData = Session["login"];
            if (null != loginData)
            {
                wxUser = loginData as WxUserInfoResult;
            }
            if (null == loginData || null == wxUser)
            {
                LogUtil.Instance.WriteInfo("[代理首页] 未登录");
                Response.Redirect(WechatUrls.GetUserInfoUrl("agent"), true);
            }
            */
            LogUtil.Instance.WriteInfo("[代理首页] 已登录 " + JSON.stringify(wxUser));
            bool isAgent = UserModels.CheckUserIsAgent(wxUser.user_id);
            isDrawFreeDiamond = UserModels.CheckUserDailyDrawDimondStatus(wxUser.user_id);
            if (!isAgent)
            {
                Response.Redirect("UserIndex.aspx?user_id=" + user_id, true); // 如果不是代理则跳转到普通用户首页
            }
            pageData = AgentIndexPageModels.GetPageData(wxUser.user_id);
            if (null == pageData)
            {
                LogUtil.Instance.WriteInfo("[代理首页] 未发现用户记录, 可能系统正在维护");
                Response.Write("未发现用户记录, 可能系统正在维护!");
                Response.End();
            }
            // 每天人工触发数据库当日统计清零操作
            UserModels.StaticsDailyReset();

        }
    }
}